package com.mingqijia.gassafety.console.controller;

import com.gap.basic.base.CommonResponse;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.log.LogRecord;
import com.mingqijia.gassafety.webserver.request.RunningReportConfigRequest;
import com.mingqijia.gassafety.webserver.request.RunningReportTemplateRequest;
import com.mingqijia.gassafety.webserver.response.RunningReportConfigResponse;
import com.mingqijia.gassafety.webserver.response.RunningReportTemplateResponse;
import com.mingqijia.gassafety.webserver.service.RunningReportConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.regex.Pattern;

import static com.mingqijia.gassafety.shared.constant.OperateRecordEnum.OPERATE_TYPE_61;
import static com.mingqijia.gassafety.shared.constant.OperateRecordEnum.OPERATE_TYPE_62;


/**
 * <p>
 * 运行报告配置
 *
 * @author mby
 * @since 2024-03-11
 */

@Slf4j
@RestController
@RequestMapping("/business/v1/runningReport/config")
@Api(tags = "运行报告配置")
public class RunningReportConfigController {

    @Autowired
    RunningReportConfigService runningReportConfigService;

    @LogRecord(operateType = OPERATE_TYPE_61, consumerId = "#request.id")
    @ApiOperation(value = "设备运行报告生成配置")
    @PostMapping("/editConfig")
    public CommonResponse<Object> editConfig(@RequestBody @Valid RunningReportConfigRequest request) {
        // 校验
        runningReportConfigService.editConfig(request);
        return CommonResponse.success("success");
    }

    @LogRecord(operateType = OPERATE_TYPE_62, consumerId = "#request.id")
    @ApiOperation(value = "设备运行报告文档模版配置")
    @PostMapping("/editTemplate")
    public CommonResponse<Object> editTemplate(@RequestBody @Valid RunningReportTemplateRequest request) {
        // 校验
        if (request.getHeaderShow() != null && request.getHeaderShow() == 0) {
            if (StringUtils.isEmpty(request.getHeaderContent()) || !Pattern.matches(Constants.REGEXP_RUNNING_REPORT_HEADER_TITLE, request.getHeaderContent())) {
                throw new BizException("请输入2-14个汉字、字母、数字、字符");
            }
        }
        runningReportConfigService.editTemplate(request);
        return CommonResponse.success("success");
    }

    @ApiOperation(value = "设备运行报告生成配置详情")
    @GetMapping("/configDetail")
    public CommonResponse<RunningReportConfigResponse> configDetail() {
        return CommonResponse.success(runningReportConfigService.configDetail());
    }

    @ApiOperation(value = "设备运行报告文档模版详情 isDefault:是否获取默认模版,true:是,false:否")
    @GetMapping("/templateDetail")
    public CommonResponse<RunningReportTemplateResponse> templateDetail(@RequestParam("isDefault") Boolean isDefault) {
        return CommonResponse.success(runningReportConfigService.templateDetail(isDefault));
    }

}
